package cn.terys.terystransactionclient;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import cn.emagsoftware.ui.ToastManager;
import cn.emagsoftware.ui.staticmanager.StaticManager;
import cn.emagsoftware.util.MathUtilities;
import cn.emagsoftware.util.StringUtilities;
import cn.terys.terystransactionclient.data.NetManager;
import cn.terys.terystransactionclient.data.db.DBManager;

public class MoneyConfirmActivity extends BaseActivity
{

    public static final String INTENT_EXTRANAME_ORDER_INFO = "ORDER_INFO";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        if (!WelcomeActivity.IS_APP_RUNNING)
        {
            StaticManager.restartAppWhenDalvikRecycled(this, WelcomeActivity.class);
            return;
        }
        setContentView(R.layout.moneyconfirm);
        setTitle(R.string.moneyconfirm_title);

        Intent intent = getIntent();
        TextView sum = (TextView) findViewById(R.id.sum);
        final String sumCentStr = intent.getStringExtra(MoneyInputActivity.INTENT_EXTRANAME_SUM);
        String sumStr = MathUtilities.round2(MathUtilities.div(Long.parseLong(sumCentStr), 100, 2), 2);
        sum.setText(Html.fromHtml("<font color='red'><b>" + sumStr + "</b></font>&nbsp;&nbsp;" + getString(R.string.generic_sum_rmb_unit)));
        TextView content = (TextView) findViewById(R.id.content);
        final String contentStr = intent.getStringExtra(MoneyInputActivity.INTENT_EXTRANAME_CONTENT);
        content.setText(contentStr);
        Button confirm = (Button) findViewById(R.id.confirm);
        confirm.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                String batchMaxTransaction = WelcomeActivity.APP_PARAM.getParameter("BatchMaxTransaction");
                if (StringUtilities.isAllCharDigit(batchMaxTransaction))
                {
                    long batchMax = Long.parseLong(batchMaxTransaction);
                    if (DBManager.getBatchTransCountFromMoney(MoneyConfirmActivity.this, MainActivity.BATCH_NUM) + 1 > batchMax)
                    {
                        ToastManager.showLong(MoneyConfirmActivity.this, StringUtilities.replaceWordsAll(getString(R.string.moneyconfirm_confirm_transcountout), "{0}", String.valueOf(batchMax)));
                        return;
                    }
                }
                new GenericAsyncTask<String, Object, String[]>(MoneyConfirmActivity.this)
                {
                    @Override
                    protected String[] doInBackgroundImpl(String... arg0) throws Exception
                    {
                        // TODO Auto-generated method stub
                        return NetManager.moneyRequest(arg0[0], arg0[1]);
                    }

                    protected void onPostExecute(Object[] objs, String[] result)
                    {
                        // TODO Auto-generated method stub
                        super.onPostExecute(objs, result);
                        Context context = (Context) objs[0];
                        Intent intent = new Intent(context, MoneyListActivity.class);
                        intent.putExtra(INTENT_EXTRANAME_ORDER_INFO, result);
                        context.startActivity(intent);
                    }
                }.execute(sumCentStr, contentStr);
            }
        });
        Button rtn = (Button) findViewById(R.id.rtn);
        rtn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                finish();
            }
        });

    }

}